﻿package code.main.view.table {
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Linear;
	
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * 倒计时动画
	 * @author YY
	 * 
	 */
    public class TimerCircle {
		private var BETTIME:int = 22;
        private var _timer:Timer = null;
        private var _tweenShow:TweenLite = null;
        private var _curtime:int = 0;
        private var _timerMask:MovieClip = null;
        private var _alumMc:MovieClip = null;
        private var _timerRotation:MovieClip = null;
        private var _isPlayTimeSound:Boolean = false;
        private var _time:int = 22;
        private var _tween:TweenLite = null;
        private var _displayObj:MovieClip = null;
        private var _rate:int = 1;
		private var _timeTip:Object;
		
		private var _cb:Function;//倒计时结束回调函数 
        public function TimerCircle(rec1:MovieClip, rec2:MovieClip):void{
            _timerMask = null;
            _timerRotation = null;
            _alumMc = null;
            _timer = null;
            _time = BETTIME;
            _curtime = 0;
            _displayObj = null;
            _isPlayTimeSound = false;
            _tween = null;
            _tweenShow = null;
            _rate = 1;
            super();
            _displayObj = rec1;
			_timeTip = rec2;
            _displayObj.stop();
            _timerMask = rec1.timerMask;
            _timerRotation = new MovieClip();
            _alumMc = rec1.alumMc;
            _displayObj.timeCircle.mask = _timerMask;
            init();
        }
		/**开始
		 * @param t
		 * @param cb
		 * 
		 */
        public function start(t:int,cb:Function):void{
			_cb = cb;
            _timer.reset();
            _timer.repeatCount = (t * _rate);
            _timer.start();
            _time = (t * _rate);
            _curtime = (t * _rate);
			timerStartVisible(_curtime);
            _displayObj.visible = true;
            _isPlayTimeSound = false;
            _timerRotation.rotation = 0;
            drawSector(_timerMask, 0, 0, 40, -360, 270);
            _tween = TweenLite.to(_timerRotation, t, {
                rotation:360,
                onUpdate:onUpdataFunction,
                onComplete:onCompleteFunction,
                ease:Linear.easeNone
            });
            tweenShow();
        }
        public function TimeSecSound():void{
//            _event.ProcessSecTimeSound();
        }
        public function onTimer(_arg1:TimerEvent):void{
            _curtime--;
            if ((_curtime % _rate) == 0){
//                _event.ProcessStopFrame((_curtime / _rate), _isPlayTimeSound);
				timerStartVisible(_curtime);
            };
            if (_curtime == int(((_time * 7) / BETTIME))){
                _alumMc.visible = true;
                _alumMc.gotoAndPlay(1);
            };
            if (_curtime == int(((_time * 6) / BETTIME))){
                TimeMilSound();
            };
            if (_curtime == int(((_time * 8) / BETTIME))){
                TimeSecSound();
            };
            if (_curtime == int(((_time * 10) / BETTIME))){
                TimeSound();
            };
        }
        private function tweenShow():void{
            _displayObj.scaleX = (_displayObj.scaleY = 0.8);
            _displayObj.alpha = 0;
            _tweenShow = TweenLite.to(_displayObj, 0.4, {
                scaleX:1,
                scaleY:1,
                alpha:1,
                ease:Back.easeOut
            });
        }
        private function onCompleteFunction():void{
            _timerRotation.rotation = -360;
        }
        public function TimeSound():void{
//            _event.ProcessTimeSound();
            _isPlayTimeSound = true;
        }
        public function onTimerComplete(_arg1:TimerEvent):void{
            stop();
			if(Boolean(_cb)) {
				_cb();
				_cb = null;
			}
        }
        public function stop():void{
            _timer.reset();
			stopTips();
            _displayObj.visible = false;
//            _event.ProcessTimerComplete();
            _timerRotation.rotation = 0;
            _alumMc.stop();
            _alumMc.visible = false;
            _displayObj.scaleX = (_displayObj.scaleY = 1);
            _displayObj.alpha = 1;
        }
        private function drawSector(_arg1:MovieClip, _arg2:Number=0, _arg3:Number=0, _arg4:Number=0, _arg5:Number=0, _arg6:Number=270, _arg7:Number=0xFF0000):void{
            var _local8:Number;
            var _local9:Number;
            var _local10:*;
            var _local11:*;
            var _local12:*;
            var _local13:*;
            var _local14:*;
            var _local15:*;
            _arg1.graphics.beginFill(_arg7, 50);
            _arg1.graphics.moveTo(_arg2, _arg3);
            _local8 = Math.ceil((Math.abs(_arg5) / 45));
            _local9 = (_arg5 / _local8);
            _local9 = ((_local9 * Math.PI) / 180);
            _arg6 = ((_arg6 * Math.PI) / 180);
            _arg1.graphics.lineTo(_arg2, (_arg3 - _arg4));
            _local10 = 1;
            while (_local10 <= _local8) {
                _arg6 = (_arg6 + _local9);
                _local11 = (_arg6 - (_local9 / 2));
                _local12 = (_arg2 + ((_arg4 / Math.cos((_local9 / 2))) * Math.cos(_local11)));
                _local13 = (_arg3 + ((_arg4 / Math.cos((_local9 / 2))) * Math.sin(_local11)));
                _local14 = (_arg2 + (_arg4 * Math.cos(_arg6)));
                _local15 = (_arg3 + (_arg4 * Math.sin(_arg6)));
                _arg1.graphics.curveTo(_local12, _local13, _local14, _local15);
                _local10++;
            };
            _arg1.graphics.endFill();
        }
        private function init():void{
			_alumMc.stop();
            var _local1:int;
            _local1 = (1000 / _rate);
            _time = (_time * _rate);
            _curtime = (_time - _rate);
            _timer = new Timer(_local1, _time);
            _timer.addEventListener(TimerEvent.TIMER, onTimer);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			stopTips();
			stop();
        }
        public function TimeMilSound():void{
//            _event.ProcessMilSound();
        }
        private function onUpdataFunction():void{
            var value:Number;
            _timerMask.graphics.clear();
			value = ((_timerRotation.rotation > 0)) ? _timerRotation.rotation : (_timerRotation.rotation + 360);
            drawSector(_timerMask, 0, 0, 40, (value - 360));
        }
		//tips
		/**
		 *显示时间 
		 * @param t
		 * 
		 */
		public function startTips(t:String):void{
			_timeTip.restTime.text = String(t);
		}
		public function timerStartVisible(t:int):void{
			_timeTip.clock.mc_animationTime.play();
			_timeTip.restTime.text = String(t);
			_timeTip.visible = true;
		}
		public function stopTips():void{
			_timeTip.clock.mc_animationTime.gotoAndStop(1);
			_timeTip.restTime.text = "";
			_timeTip.visible = false;
		}
    }
}